package com.ww.springboot.boot.redisson.ratelimiter;

import com.ww.springboot.boot.function.CheckedSupplier;
import com.ww.springboot.boot.util.Exceptions;

import java.util.concurrent.TimeUnit;

public interface RateLimiterClient {
    default boolean isAllowed(String key, long max, long ttl) {
        return this.isAllowed(key, max, ttl, TimeUnit.SECONDS);
    }

    boolean isAllowed(String key, long max, long ttl, TimeUnit timeUnit);

    default <T> T allow(String key, long max, long ttl, CheckedSupplier<T> supplier) throws Throwable {
        return this.allow(key, max, ttl, TimeUnit.SECONDS, supplier);
    }

    default <T> T allow(String key, long max, long ttl, TimeUnit timeUnit, CheckedSupplier<T> supplier) throws Throwable {
        boolean isAllowed = this.isAllowed(key, max, ttl, timeUnit);
        if (isAllowed) {
            try {
                return supplier.get();
            } catch (Throwable var10) {
                throw Exceptions.unchecked(var10);
            }
        } else {
            throw new RateLimiterException(key, max, ttl, timeUnit);
        }
    }
}
